package Stdlog::Writer::Syslog;

use 5.008;
use strict;
use warnings;
use Hash::Util qw(lock_keys);
use Sys::Syslog qw(:standard :macros);

my %writer_cache;

my %syslog_priorities = (
    'FATAL'  => LOG_ERR,
    'ERROR'  => LOG_WARNING,
    'NOTICE' => LOG_NOTICE,
    'INFO'   => LOG_INFO,
    'DEBUG'  => LOG_DEBUG
);


sub new {
    my ($pkg, $conf) = @_;
    my $self = {
        'fh'              => undef,
        'err'             => '',
        'syslog_ident'    => $conf->{syslog_ident} || $0,
        'syslog_facility' => $conf->{syslog_facility} || 'user',
        'syslog_opts'     => $conf->{syslog_opts} || 'nodelay',
        'opened'          => 0,
    };
    bless $self, $pkg;
    lock_keys(%$self);
    return $self;
}


sub logmsg {
    my $self = shift @_;
    my $args = shift @_;

    if( ! $self->{opened} ){
        openlog($self->{syslog_ident}, $self->{syslog_opts}, $self->{syslog_facility} );
    }
    my $prior = $syslog_priorities{$args->{severity}} || LOG_INFO;
    syslog($prior, "$_\n");
}


sub DESTROY {
    my $self = shift @_;
    closelog();
}

1;
__END__

=head1 NAME

Stdlog::Writer::File -- file output for stdlog

=head1 SYNOPSIS

  use Stdlog;
  
  

=head1 DESCRIPTION


=head1 CLASS METHODS

=head2 Stdlog::Writer->new(CLASS, OPTS)


=head2 Stdlog::Writer->get(ID)


=head1 OBJECT METHODS


=head1 EXPORTS

None by default.


=head1 SEE ALSO


=head1 AUTHOR

Robert Mah E<lt>rmah@pobox.comE<gt>


=head1 COPYRIGHT AND LICENSE

Copyright (C) 2007 by Robert Mah

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut
